package com.fs.service.impl;


import com.fs.constant.CacheConstants;
import com.fs.entity.Admins;
import com.fs.entity.User;
import com.fs.util.RedisCache;
import com.fs.util.ResponseData;
import com.fs.util.ResponseDataUtil;
import com.fs.util.ResultEnums;
import com.fs.util.sign.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 登录密码方法
 */
@Service
public class PasswordService
{
    @Autowired
    private RedisCache redisCache;

    @Value(value = "${user.password.maxRetryCount}")
    private int maxRetryCount;

    @Value(value = "${user.password.lockTime}")
    private int lockTime;

    /**
     * 登录账户密码错误次数缓存键名
     *  pwd_err_cnt:admin
     * @param username 用户名
     * @return 缓存键key
     */
    private String getCacheKey(String username){
        return CacheConstants.PWD_ERR_CNT_KEY + username;
    }

    public ResponseData validate(Admins admins, String password) {
        String username  = admins.getUsername();

        //获取从redis中保存的pwd_err_cnt:用户名  对应的访问登录验证次数
        Integer retryCount = redisCache.getCacheObject(getCacheKey(username));

        //第一次登录么有reids值，设置成0
        if (retryCount == null) {
            retryCount = 0;
        }

        //如果登录错误次数大于redis记录的错误次数，用户错误最大次数异常类
        if (retryCount >= Integer.valueOf(maxRetryCount).intValue()) {
            return ResponseDataUtil.buildError(400,"密码输入错误"+maxRetryCount+"次，帐户锁定"+lockTime+"分钟");
        }

        //检验密码是否匹配
        if (!matches(admins, password))
        {
            //密码如果不匹配，记录错误次数到redis中
            retryCount = retryCount + 1;
            redisCache.setCacheObject(getCacheKey(username), retryCount, lockTime, TimeUnit.MINUTES);
            return ResponseDataUtil.buildError(400,"密码错误");
        }
        else{
            //密码如果匹配成功，从redis中移除记录的登录错误次数
            clearLoginRecordCache(username);
            return ResponseDataUtil.buildOk(ResultEnums.OK);
        }
    }

    public ResponseData validate(User user, String password) {
        String username  = user.getUsername();

        //获取从redis中保存的pwd_err_cnt:用户名  对应的访问登录验证次数
        Integer retryCount = redisCache.getCacheObject(getCacheKey(username));

        //第一次登录么有reids值，设置成0
        if (retryCount == null) {
            retryCount = 0;
        }

        //如果登录错误次数大于redis记录的错误次数，用户错误最大次数异常类
        if (retryCount >= Integer.valueOf(maxRetryCount).intValue()) {
            return ResponseDataUtil.buildError(400,"密码输入错误"+maxRetryCount+"次，帐户锁定"+lockTime+"分钟");
        }

        //检验密码是否匹配
        if (!matches(user, password))
        {
            //密码如果不匹配，记录错误次数到redis中
            retryCount = retryCount + 1;
            redisCache.setCacheObject(getCacheKey(username), retryCount, lockTime, TimeUnit.MINUTES);
            return ResponseDataUtil.buildError(400,"密码错误");
        }
        else{
            //密码如果匹配成功，从redis中移除记录的登录错误次数
            clearLoginRecordCache(username);
            return ResponseDataUtil.buildOk(ResultEnums.OK);
        }
    }

    public boolean matches(Admins admins, String rawPassword){
        return PasswordUtils.verify(rawPassword,admins.getPwd());
    }

    public boolean matches(User user, String rawPassword){
        return PasswordUtils.verify(rawPassword,user.getPassword());
    }

    public void clearLoginRecordCache(String loginName){
        if (redisCache.hasKey(getCacheKey(loginName))){
            redisCache.deleteObject(getCacheKey(loginName));
        }
    }
}